/**
 * 
 */
package parser.nodes;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import parser.AstNode;
import parser.Memory;
import parser.StringFinder;
import parser.StringMatch;
import parser.StringMatchSet;

/**
 * @author greg
 *
 */
public class RecursiveReplaceNode extends AstNode {

	private String regex;
	private String replacement;
	private String inputFileName;
	private String outputFileName;
	
	
	
	public void setRegex(String regex) {
		this.regex = regex;
	}
	public String getRegex() {
		return regex;
	}
	public void setReplacement(String replace) {
		this.replacement = replace;
	}
	public String getReplacement() {
		return replacement;
	}
	public void setInputFileName(String inputFileName) {
		this.inputFileName = inputFileName;
	}
	public String getInputFileName() {
		return inputFileName;
	}
	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}
	public String getOutputFileName() {
		return outputFileName;
	}
	/* (non-Javadoc)
	 * @see parser.AstNode#executeNode(parser.Memory, java.lang.Object, java.lang.Object)
	 */
	@Override
	protected Object executeNode(HashMap<String,Object> mem, Object lval, Object rval) {
		System.out.println("Replace Node.\nReplacement="+replacement+"\nInFile="+inputFileName+"\nOutFile="+outputFileName);
		StringFinder sf = new StringFinder(regex,inputFileName);
		StringMatchSet foundSet = sf.findStrings();
		
		try {
			BufferedReader fileReader = new BufferedReader(new FileReader(inputFileName));
			BufferedWriter fileWriter = new BufferedWriter(new FileWriter(outputFileName));
			
			String line;
			int lineNumber = 0;
			if((line=fileReader.readLine())!=null) {
				System.out.println("Replacing line: "+line);
				for(StringMatch sm : foundSet.matchSet) {
					if(lineNumber != sm.lineNumber) continue;
					
					System.out.println("Replacing token " + sm.toString());
					// replace the token
					line = line.substring(0,sm.startIndex)+replacement+line.substring(sm.endIndex,line.length());
				}
				lineNumber++;
				fileWriter.write(line+"\n");
				recursiveReplace(foundSet,fileReader,fileWriter,lineNumber,replacement);
			}

			fileWriter.flush();
		} catch(FileNotFoundException e1) {
			e1.printStackTrace();
		} catch(IOException e2) {
			e2.printStackTrace();
		}
			
		return null;
	}
	public void recursiveReplace(StringMatchSet foundSet,BufferedReader fileReader,BufferedWriter fileWriter,int lineNumber,String replacement)
	{
		String line;
		try {
		if((line=fileReader.readLine())!=null) {
			System.out.println("Replacing line: "+line);
			for(StringMatch sm : foundSet.matchSet) {
				if(lineNumber != sm.lineNumber) continue;
				
				System.out.println("Replacing token " + sm.toString());
				// replace the token
				line = line.substring(0,sm.startIndex)+replacement+line.substring(sm.endIndex,line.length());
			}
			lineNumber++;
			fileWriter.write(line+"\n");
			recursiveReplace(foundSet,fileReader,fileWriter,lineNumber,replacement);
		}
		} catch(FileNotFoundException e1) {
			e1.printStackTrace();
		} catch(IOException e2) {
			e2.printStackTrace();
		}
	}

}
